//
//  Seach_FiltrateBottomDef_View_WX.m
//  ZITHER
//
//  Created by weixin on 2019/3/21.
//  Copyright © 2019 weixn. All rights reserved.
//kBottomSafeHeight

#import "Seach_FiltrateBottomDef_View_WX.h"
#import "Seach_SearchResult_VC_WX.h"

@implementation Seach_FiltrateBottomDef_View_WX

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //        self.backgroundColor=  [UIColor redColor];
        
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.resetBtn];
        [self addSubview:self.doneBtn];
        [self addSubview:self.scr];
        [self.scr mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self).offset(0);
            make.top.equalTo(self).offset(1);
            make.height.mas_equalTo(50);
        }];
        [self.resetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(0);
            make.bottom.equalTo(self).offset(0);
            make.height.mas_equalTo(50);
            make.width.mas_equalTo(110);
        }];
        [self.doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.resetBtn.mas_right).offset(0);
            make.right.equalTo(self).offset(0);
            make.height.mas_equalTo(50);
            make.bottom.equalTo(self).offset(0);
        }];
        
        BaseView *view = [[BaseView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 1)];
        view.backgroundColor = HexColor(0xEEEEEE);
        [self addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self.resetBtn).offset(0);
            make.height.mas_equalTo(1);
        }];
        
        BaseView *view1 = [[BaseView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 1)];
        view1.backgroundColor = HexColor(0xEEEEEE);
        [self addSubview:view1];
        
        [self Kvo];
    }
    return self;
}

-(void)Kvo{
    
    [RACObserve(kUSER_Filtrate_MANAGER,arrBrandSelectAdd)subscribeNext:^(id  _Nullable x) {
        NSLog(@"新增元素/n%@",x);
        if (![[x class] isEqual:[Seach_FiltrateAttributeSubData_Model_WX class]]) {
            return ;
        }
        Seach_FiltrateAttributeSubData_Model_WX * modelX =  x;
        
        for (Seach_FiltrateAttributeSub_Model_WX *childModel in kUSER_Filtrate_MANAGER.arrAttribute[0].child) {
            for (Seach_FiltrateAttributeSubData_Model_WX *dataModel in childModel.data) {
                if ([modelX.labelName isEqualToString:dataModel.labelName ]) {
                    dataModel.isSelect = modelX.isSelect;
                }
            }
        }
        
        
        if (!modelX.isSelect) {
            return;
        }
        for (Seach_FiltrateAttributeSubData_Model_WX *selectModel in kUSER_Filtrate_MANAGER.arrBrandSelect) {
            if ([selectModel.labelName isEqualToString:modelX.labelName]) {
                [self addScrImage:kUSER_Filtrate_MANAGER.arrBrandSelect];
                [self refload];
                return;
            }
        }
        
        
        Seach_FiltrateAttributeSubData_Model_WX *mdoel = x;
        [kUSER_Filtrate_MANAGER.arrBrandSelect addObject:mdoel];//新增加了一条属性
        NSLog(@"%@",kUSER_Filtrate_MANAGER.arrBrandSelect);
        [self addScrImage:kUSER_Filtrate_MANAGER.arrBrandSelect];
        [self refload];
        
    }];
    
    [RACObserve(kUSER_Filtrate_MANAGER,arrBrandSelectRem)subscribeNext:^(id  _Nullable x) {
        //删除了一条属性
        NSLog(@"删除元素/n%@",x);
        
        Seach_FiltrateAttributeSubData_Model_WX *mdoel = kUSER_Filtrate_MANAGER.arrBrandSelectRem;
        
        NSMutableArray *arrM = kUSER_Filtrate_MANAGER.arrBrandSelect;
        
        Seach_FiltrateAttributeSubData_Model_WX * modelX =  x;
        
        for (Seach_FiltrateAttributeSub_Model_WX *childModel in kUSER_Filtrate_MANAGER.arrAttribute[0].child) {
            for (Seach_FiltrateAttributeSubData_Model_WX *dataModel in childModel.data) {
                if ([modelX.labelName isEqualToString:dataModel.labelName ]) {
                    dataModel.isSelect = modelX.isSelect;
                }
            }
        }
        
        for (int i =0 ; i<arrM.count; i++) {
            Seach_FiltrateAttributeSubData_Model_WX *mdoel1 = arrM[i];
            if (
                [mdoel.labelValue[0] isEqualToString:mdoel1.labelValue[0]]&&mdoel1.ispublicPrice==mdoel.ispublicPrice) {
                [kUSER_Filtrate_MANAGER.arrBrandSelect removeObjectAtIndex:i];
                break;
            }
        }
        
        [arrM removeObject:x];//删除了一条属性
        [self addScrImage:kUSER_Filtrate_MANAGER.arrBrandSelect];
        [self refload];
    }];
    [RACObserve(kUSER_Filtrate_MANAGER,SearchResultTopBarModel.ResultCont)subscribeNext:^(id  _Nullable x) {
        
        int officialPriceeInt = [kUSER_Filtrate_MANAGER.SearchResultTopBarModel.ResultCont intValue];
        NSNumberFormatter *moneyFormatter = [[NSNumberFormatter alloc] init];
        moneyFormatter.positiveFormat = @"###,##0";
        NSString *formatString = [moneyFormatter stringFromNumber:@(officialPriceeInt)];
        [self.doneBtn setTitle:[NSString stringWithFormat:@"%@款",formatString] forState:UIControlStateNormal];
    }];
    
    
    
}

//每次添加修改 都要刷新所有数据源
-(void)refload{
    if (self.State == 0) {
        
        NSLog(@"当前选择属性/n%@",kUSER_Filtrate_MANAGER.arrBrandSelect);
        [[serviceDefualt loadsearchGoodsCount:kUSER_Filtrate_MANAGER.arrBrandSelect reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
            [self setDic:x];
        }];
    }else{
        [[serviceDefualt loadsearchGoodsCount:kUSER_Filtrate_MANAGER.arrBrandSelect keyword:kUSER_Filtrate_MANAGER.SearchResultTopBarModel.keyWord reponseObjctClass:nil]subscribeNext:^(id  _Nullable x) {
            [self setDic:x];
        }];
    }
}

-(void)setDic:(ResponseData*)x{
    if (x.success) {
        int officialPriceeInt = [x.responseRawData[@"data"] intValue];
        
        NSNumberFormatter *moneyFormatter = [[NSNumberFormatter alloc] init];
        moneyFormatter.positiveFormat = @"###,##0";
        NSString *formatString = [moneyFormatter stringFromNumber:@(officialPriceeInt)];
        [self.doneBtn setTitle:[NSString stringWithFormat:@"%@款",formatString] forState:UIControlStateNormal];
        kUSER_Filtrate_MANAGER.SearchResultTopBarModel.ResultCont = x.responseRawData[@"data"];
    }else{
        [Tools showTips:x.errorInfo];
    }
}

- (BaseBtn *)resetBtn{
    if (!_resetBtn) {
        _resetBtn = [BaseBtn initCustomBtnTitlestype:@[@"重置"] color:@[HexColor(0x333333)]];
        
    }
    return _resetBtn;
}

- (BaseBtn *)doneBtn{
    if (!_doneBtn) {
        _doneBtn = [BaseBtn initCustomBtnImageAndTitles:@[@"888，888款"] images:@[@"搜索_筛选_确认"] color:@[[UIColor whiteColor]]];
        _doneBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_doneBtn setImageEdgeInsets:UIEdgeInsetsMake(0, kScreenW-125, 0,-(kScreenW-125))];
        _doneBtn.backgroundColor = baseColorTheme;
    }
    return _doneBtn;
}

- (NSMutableArray<Seach_FiltrateBrand_Btn_WX *> *)arrBtn{
    if (!_arrBtn) {
        _arrBtn = [[NSMutableArray alloc]init];
    }
    return _arrBtn;
}

-(BaseScrollView *)scr{
    if (!_scr) {
        _scr = [[BaseScrollView alloc]init];
        //        _scr.backgroundColor = RandomColor;
    }
    return _scr;
}

-(void)addScrImage:(NSArray *)arr{
    [self.scr.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    if (arr.count == 0 || arr == nil) {
        //        self.scr.hidden = YES;
        
        return;
    }
    
    CGFloat width = (kScreenW-50)/3.0;
    
    //    self.scr.hidden = NO;
    self.scr.contentSize = CGSizeMake((arr.count-1)*(width+10)+30+width,0);
    self.scr.userInteractionEnabled = YES;
    
    for (int i = 0; i<arr.count; i++) {
        Seach_FiltrateBrand_Btn_WX *btn = [[Seach_FiltrateBrand_Btn_WX alloc]init];
        [btn setTitle:@"劳力士" forState:UIControlStateNormal];
        [btn setTitleColor:HexColor(0x333333) forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:13];
        btn.frame = CGRectMake(i*(width+10)+15, 5,width , 35);
        btn.backgroundColor = HexColor(0xf5f5f5);
        [btn setCornerRadiusWithRadius:4 isMasks:YES];
        Seach_FiltrateAttributeSubData_Model_WX *mode1 = arr[i];
        [btn changBtnisSelect:mode1.isSelect];
        [btn setTitle:mode1.labelName forState:UIControlStateNormal];
        
        [[btn addTag]subscribeNext:^(id  _Nullable x) {
            Seach_FiltrateAttributeSubData_Model_WX *model = arr[i];
            model.isSelect = NO;
            kUSER_Filtrate_MANAGER.arrBrandSelectRem = model;
            NSArray *arr =  kUSER_Filtrate_MANAGER.arrAttribute[0].child[model.indexPath.section].data;
            for (int i = 0; i< arr.count; i++) {
                Seach_FiltrateAttributeSubData_Model_WX *model3 = arr[i];
                if ([model.labelValue[0] isEqualToString:model3.labelValue[0]]) {
                    model3 = model;
                    break;
                }
            }
            
            if (self.block) {
                self.block(model.indexTbv);
            }
        }];
        [self.scr addSubview:btn];
        [_arrBtn addObject:btn];
        
    }
    
}

@end
